package com.yiren.manager.ctrl.department;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrCaregiverGrade;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.department.YrCaregiverGradeForm;
import com.yiren.manager.service.iface.department.YrCaregiverGradeService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("caregiverGrade")
@Controller
public class YrCaregiverGradeController extends GenericController<YrCaregiverGradeForm, YrCaregiverGrade>{

	@Autowired
	private YrCaregiverGradeService selfService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrCaregiverGradeForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrCaregiverGradeForm> result = new HTTPResult<YrCaregiverGradeForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrCaregiverGradeForm form = tool.mapper("caregiverGrade", YrCaregiverGradeForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getCaregiverGradeId()||form.getCaregiverGradeId().isEmpty()){
			form.setCaregiverGradeId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrCaregiverGrade entity = ObjectUtils.objectToObject(form, YrCaregiverGrade.class);
		selfService.save(entity);
		
		return result;
	}
	
	@Override
	public YrCaregiverGradeForm toForm(YrCaregiverGrade e) throws Exception {
		YrCaregiverGradeForm form = ObjectUtils.objectToObject(e, YrCaregiverGradeForm.class);
		return form;
	}

	@Override
	public YrCaregiverGrade toEntity(YrCaregiverGradeForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
